package com.mayikt.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mayikt.edu.constant.MayiktConstants;
import com.mayikt.edu.core.cache.LocalCache;
import com.mayikt.edu.dto.resp.EduCourseClassRespDTO;
import com.mayikt.edu.dto.resp.EduCourseRespDTO;
import com.mayikt.edu.entity.EduCourse;
import com.mayikt.edu.entity.EduCourseClass;
import com.mayikt.edu.service.IEduCourseClassService;
import com.mayikt.edu.service.IEduCourseService;
import com.mayikt.edu.service.IStartPreheatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class IStartPreheatServiceImpl implements IStartPreheatService {
    @Autowired
    private IEduCourseClassService iEduCourseClassService;
    @Autowired
    private IEduCourseService iEduCourseService;

    @Override
    public void initData() {
        Long startTime = System.currentTimeMillis();
        // 1.初始化课程分类加入缓存
        initEduCourseClass();
        // 2 初始化课程列表加入本地缓存
        initEduCourseList();
        Long endTime = System.currentTimeMillis();
        log.info("[缓存预热的时间：]" + (endTime - startTime));
    }

    /**
     * 初始化课程列表
     */
    private void initEduCourseList() {
        List<EduCourse> allEduCourseList = iEduCourseService.getAllEduCourse();
        List<EduCourseRespDTO> eduCourseClassRespDTOList =
                BeanUtil.copyToList(allEduCourseList, EduCourseRespDTO.class);

        LocalCache.put(MayiktConstants.COURSE_EDUCOURSERESPLIST, eduCourseClassRespDTOList);
        log.info("1.初始化数据【allCourseClassList：{}】 完成", eduCourseClassRespDTOList);
    }

    /**
     * 初始化课程分类
     */
    private void initEduCourseClass() {
        List<EduCourseClass> allCourseClassList = iEduCourseClassService.getAllCourseClass();
        // 将do转为dto
        List<EduCourseClassRespDTO> eduCourseClassRespDTOList =
                BeanUtil.copyToList(allCourseClassList, EduCourseClassRespDTO.class);

        LocalCache.put(MayiktConstants.COURSE_ALLCOURSECLASSLIST, eduCourseClassRespDTOList);
        log.info("1.初始化数据【allCourseClassList：{}】 完成", eduCourseClassRespDTOList);
    }
}
